﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MobileMake.Models.Validation;
using System.Collections.Specialized;

namespace MobileMake.Models
{
    public class DataService : IDataService  
    {
        #region Miembros Privados
        private IValidationDictionary _validationDictionary;
        private IDataProvider _dataProvider; 
        #endregion

        #region Constructs
        public DataService(IValidationDictionary validationDictionary)
            : this(validationDictionary, new EntityDataProvider()) 
        { }

        public DataService(IValidationDictionary validationDictionary, IDataProvider dataProvider)
        {
            _validationDictionary = validationDictionary;
            _dataProvider = dataProvider;
        } 
        #endregion

        #region Portals

        public bool ValidatePortal(Portal portalToValidate)
        {
            if (portalToValidate.PortalName.Length == 0)
                _validationDictionary.AddError("PortalName", "Portal Name is required.");
            if (portalToValidate.Description.Length > 500)
                _validationDictionary.AddError("Description", "Length not max 500.");
            if (portalToValidate.KeyWords.Length > 500)
                _validationDictionary.AddError("KeyWords", "Length not max 500.");
            return _validationDictionary.IsValid;
        }
        
        public bool AddPortal(Portal portalToAdd)
        {
            if (!ValidatePortal(portalToAdd)) return false;

            try
            {
                portalToAdd = _dataProvider.AddPortal(portalToAdd);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool DeletePortal(Portal portalToDelete)
        {
            try
            {
                _dataProvider.DeletePortal(portalToDelete);
            }
            catch
            {
                _validationDictionary.AddError("Delete", "Record not deleted. Error in db");
                return false;
            }
            return true;
        }

        public bool UpdatePortal(Portal portalToUpdate)
        {
            if (!ValidatePortal(portalToUpdate)) return false;

            try
            {
                portalToUpdate = _dataProvider.UpdatePortal(portalToUpdate);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public Portal GetPortal(int? portalId)
        {
            if (!portalId.HasValue)
            {
                _validationDictionary.AddError("Id", "The id is invalid.");

                return new Portal();
            }

            return _dataProvider.GetPortal(portalId.Value);
        }       

        public List<Portal> GetPortals()
        {
            return _dataProvider.GetPortals();
        }

        public IQueryable<Portal> GetPortalsQueryable()
        {
            return _dataProvider.GetPortals().AsQueryable();
        }
        
        #endregion
        
    }
}
